package com.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pojo.Student;
import com.service.StudentService;
import com.vo.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
@RestController
@CrossOrigin("*")
@RequestMapping("/student")
public class StudentAction {
    @Autowired
    private StudentService studentService;

    /**
     *  分页查询所有学生
     * @param page   第几页
     * @param limit  一页几条信息
     * @param m   搜索参数
     * @return
     */
    @RequestMapping("/searchStudent")
    public ResponseBean searchStudent(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10") int limit,@RequestParam Map m){
        IPage iPage = studentService.searchStudent(page,limit,m);
        return new ResponseBean(0,iPage.getTotal(),iPage.getRecords());
    }


    /**
     *  根据账号查学生信息
     * @param username  账号
     * @return
     */
    @RequestMapping("/queryStudent")
    public ResponseBean queryStudent(String username){
        // 根据账号查学生信息
        Student student=studentService.queryStudent(username);
        return new ResponseBean(0,0,student);
    }

    /**
     *  修改学生信息
     * @param student  学生信息
     * @return
     */
    @RequestMapping("/updateStudent")
    public ResponseBean updateStudent(Student student){
        // 修改学生信息
        int n=studentService.updateStudent(student);
        return new ResponseBean(0,0,n);
    }

    @RequestMapping("/addOrUpdateStudent")
    public ResponseBean addOrUpdateStudent(@RequestParam Map map){
        Student student= JSONObject.parseObject(JSON.toJSONString(map), Student.class);
        return new ResponseBean(200,0,studentService.saveOrUpdate(student));
    }
}
